import base64
import os
import re
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.support.ui import WebDriverWait
from lxml import etree




def parse_dynamic_content(url):
    print(f"Parsing dynamic content from: {url}")
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument('--ignore-certificate-errors')
    options.add_argument('--disable-web-security')
    driver = webdriver.Chrome(options=options)
    print('1')
    try:
        driver.get(url)
        print('2')
        # 等待JavaScript执行完成
        WebDriverWait(driver, 10).until(
            lambda d: d.execute_script('return document.readyState') == 'complete'
        )
        print('23')
        # 获取渲染后的HTML
        html = driver.page_source
        

        s = str(html)
        idx = s.find("男人向往的好职业啊")
        if idx != -1:
            result = s[idx + len("男人向往的好职业啊"):idx + len("男人向往的好职业啊") + 222000]
            print(result)

        #tree = etree.HTML(html)
        #print(tree)
        #title = tree.xpath('//*')
        soup = BeautifulSoup(html, 'lxml')
        img_tag = soup.body
        #print(img_tag.find_all('img', attrs={'data-xuid': '2'}))
        #print(type(img_tag.find_all('img', attrs={'data-xuid': '12'})))
        srcs = []
        # 使用正则表达式查找所有符合条件的图片链接
        for img in img_tag.find_all('img', attrs={'data-xuid': re.compile(r'^\d+$')}):
            print(img)  # 输出整个img标签
            print(img.get('src'))  # 输出src属性
            srcs.append(img.get('src'))
            print(img.get('data-xkrkllgl'))  # 输出data-xkrkllgl属性
        #.div.find('img', attrs={'data-xuid': '1'})
        #print('标题是' + str(img_tag))
        #print(title)
        #soup(str(soup))
        # 解析动态加载的内容
        # dynamic_items = [
        #     item.text for item in soup.select('.dynamic-content')
        # ]
        for i, base64_str in enumerate(srcs, 1):
            # 如果有前缀（如 data:image/png;base64,），去掉前缀
            if ',' in base64_str:
                base64_data = base64_str.split(',', 1)[1]
            else:
                base64_data = base64_str

            img_data = base64.b64decode(base64_data)
            if not os.path.exists('乌干娜图片集'):
                os.mkdir('乌干娜图片集')
            with open(f'./乌干娜图片集/乌干娜{i}.png', 'wb') as f:
                f.write(img_data)
            print(f'乌干娜{i}已保存')
        print('------------end--------------')
        return 'dynamic_items'
    finally:
        driver.quit()


print(parse_dynamic_content('https://all.gfvfkxno.club/archives/154281/'))
